﻿using AutoMapper.Internal.Mappers;
using CSRedis;
using EMS.system.Entityies.BaseData;
using EMS.system.Entityies.MasterData.CustomerManagementModel;
using EMS.system.IEntityAppService.MasterData.CustomerManagementIService;
using EMS.system.IEntityAppService.MasterData.CustomerManagementIService.CustomerManagementDTO;
using IdentityModel.OidcClient;
using Microsoft.AspNetCore.Mvc;
using NuGet.Protocol.Core.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace EMS.system.Web.Controllers.MasterData.CustomerManagements
{
    /// <summary>
    /// 客户管理
    /// </summary>
    public class CustomerManagementController : Controller
    {
        private readonly ICustomerManagementAppService service;
       

        public CustomerManagementController(ICustomerManagementAppService service)
        {
            this.service = service;
        }

        public IActionResult CustomerManagementShow()
        {
            return View();
        }

        public IActionResult CustomerManagementAdd()
        {
            return View();
        }

        public IActionResult Sel()
        {
            return View();
        }

        [HttpPost]
        public async Task<int> AddCustomerManagement(CustomerManagementInsertDTO dto)
        {
            var list = await service.AddCustomerManagement(dto);
            return list;
        }

        [HttpGet]
        public JsonResult GetCustomerManagement(string? code, string? name, string? forshort, int page, int limit)
        {
            var list = service.GetCustomerManagement(code, name, forshort, page, limit);
            return Json(new { data = list.Queryable, count = list.RowCount, code = "0", msg = "" });
        }


        [HttpPost]
        public int DeleteCustomerManagement(int id)
        {
            var res = service.DeleteCustomerManagement(id);
            return res;
        }


        [HttpGet]
        public JsonResult GetCustomerById(int id)
        {
            var customer = service.GetCustomerById(id);
            return Json(new { code = 0, data = customer });
        }

        [HttpPost]
        public async Task<int> UpdateCustomer(CustomerManagementUpdateDTO dto)
        {
            var list = await service.UpdateCustomer(dto);
            return list;
        }

        [HttpPost]
        public int DeleAll(string ids)
        {
            int res = 0;
            foreach (var id in ids.Split(","))
            {
                res = service.DeleteCustomerManagement(int.Parse(id));
            }
            return res;
        }

        public JsonResult GetDictItem(int id=22)
        {
            var list = service.GetDictItem(id);
            return Json(new { code = 0, data = list });
        }



        
    }
}
